<?php
/**
 * Created by yb.
 * Author: 1021948372@qq.com
 * Date: 2020/7/7 07:07
 */
namespace app\common\controller;

use app\common\lib\SessionCheck;
use app\common\lib\GlobalConfig;
use app\common\model\SystemConfig as SystemConfigModel;

class SystemConfig extends SessionCheck
{

    //统一页面
    public function setting(){
        $flags = array(GlobalConfig::WEB_CONTENT,GlobalConfig::WECHAT_OAUTH_NAME,GlobalConfig::WECHAT_MIAN_OAUTH_NAME,GlobalConfig::ALIPAY_PAY,GlobalConfig::MAILER,GlobalConfig::ALIYUN_SMS);
        $data = model('SystemConfig')->where('flag','in',$flags)->select()->toArray();

        foreach($data as $k => &$v){
            if($v && $v['is_serialize']){
                $v['value'] = unserialize($v['value']);
            }
        }
        $data = array_column($data,null,'flag');
        $this->assign('data',$data);
        $this->assign('path','SystemConfig/wxMinaOauth');
        return $this->fetch('common@system_config/third_config');
    }

    /**
     * ----------------------------------------------------------------------
     * 系统配置
     * ---------------------------------------------------------------------
     */

    /**
     * 网站配置
     */
    public function webContent(){
        $title = '网站内容配置';
        $flag = GlobalConfig::WEB_CONTENT;
        if(request()->isPost()){
            $value = array(
                'name' => input('name',''),
                'domain' => input('domain',''),
                'title' => input('title',''),
                'keywords' => input('keywords',''),
                'description' => input('description',''),
                'author' => input('author',''),
                'copyright' => input('copyright',''),
            );
            $saveData = array(
                'title' => $title,
                'flag' => $flag,
                'value' => serialize($value),
                'is_serialize' => 1
            );
            //先删后增
            db('system_config')->where(array('flag' => $flag))->delete();
            db('system_config')->insert($saveData);

            //删除配置缓存
            $this->delSystemConfig();

            $this->success('操作成功');
        }else{
            $this->assign('title',$title);
            $this->assign('config_flag',$flag);
            //获取值
            $data = db('system_config')->where(array('flag' => $flag))->find();
            if($data && $data['is_serialize']){
                $data['value'] = unserialize($data['value']);
            }
            $this->assign('data',$data);
            return $this->fetch('common@system_config/web_content');
        }
    }

    /**
     * ----------------------------------------------------------------------
     * 第三方服务配置
     * ---------------------------------------------------------------------
     */

    /**
     * 微信公众号授权参数
     */
    public function wxOauth(){
        $title = '微信公众号设置';
        $flag = GlobalConfig::WECHAT_OAUTH_NAME;
        if(request()->isPost()){
            $value = array(
                'name' => input('name',''),
                'appid' => input('appid',''),
                'appsecret' => input('appsecret',''),
                'mch_id' => input('mch_id',''),
                'key' => input('key',''),
            );
            $saveData = array(
                'title' => $title,
                'flag' => $flag,
                'value' => serialize($value),
                'is_serialize' => 1
            );
            //先删后增
            db('system_config')->where(array('flag' => $flag))->delete();
            db('system_config')->insert($saveData);

            //删除配置缓存
            $this->delSystemConfig();

            $this->success('操作成功');
        }else{
            $this->assign('title',$title);
            $this->assign('config_flag',$flag);
            //获取值
            $data = db('system_config')->where(array('flag' => $flag))->find();
            if($data && $data['is_serialize']){
                $data['value'] = unserialize($data['value']);
            }
            $this->assign('data',$data);
            return $this->fetch('common@system_config/wx_oauth');
        }
    }

    /**
     * 微信小程序授权参数
     */
    public function wxMinaOauth(){
        $title = '微信小程序设置';
        $flag = GlobalConfig::WECHAT_MIAN_OAUTH_NAME;
        if(request()->isPost()){
            $value = array(
                'name' => input('name',''),
                'appid' => input('appid',''),
                'appsecret' => input('appsecret',''),
                'mch_id' => input('mch_id',''),
                'key' => input('key',''),
            );
            $saveData = array(
                'title' => $title,
                'flag' => $flag,
                'value' => serialize($value),
                'is_serialize' => 1
            );
            //先删后增
            db('system_config')->where(array('flag' => $flag))->delete();
            db('system_config')->insert($saveData);

            //删除配置缓存
            $this->delSystemConfig();

            $this->success('操作成功');
        }else{
            $this->assign('title',$title);
            $this->assign('config_flag',$flag);
            //获取值
            $data = db('system_config')->where(array('flag' => $flag))->find();
            if($data && $data['is_serialize']){
                $data['value'] = unserialize($data['value']);
            }
            $this->assign('data',$data);
            return $this->fetch('common@system_config/wx_mina_oauth');
        }
    }

    /**
     * 支付宝支付参数
     */
    public function alipay(){
        $title = '支付宝配置';
        $flag = GlobalConfig::ALIPAY_PAY;
        if(request()->isPost()){
            $value = array(
                'app_id' => input('app_id',''),
                'merchant_private_key' => input('merchant_private_key',''),
                'merchant_public_key' => input('merchant_public_key',''),
                'alipay_public_key' => input('alipay_public_key',''),
                'notify_url' => input('notify_url',''),
                'charset' => 'UTF-8',
                'sign_type' => 'RSA2',
                'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
            );
            $saveData = array(
                'title' => $title,
                'flag' => $flag,
                'value' => serialize($value),
                'is_serialize' => 1,
            );
            //先删后增
            db('system_config')->where(array('flag' => $flag))->delete();
            db('system_config')->insert($saveData);

            //删除配置缓存
            $this->delSystemConfig();

            $this->success('操作成功');
        }else{
            $this->assign('title',$title);
            $this->assign('config_flag',$flag);
            //获取值
            $data = db('system_config')->where(array('flag' => $flag))->find();
            if($data && $data['is_serialize']){
                $data['value'] = unserialize($data['value']);
            }
            $this->assign('data',$data);
            return $this->fetch('common@system_config/alipay');
        }
    }

    /**
     * 发送邮箱
     */
    public function mailer(){
        $title = '邮箱配置';
        $flag = GlobalConfig::MAILER;
        if(request()->isPost()){
            $value = array(
                'MAIL_SMTP' => TRUE,
                'MAIL_FROMNAME' => input('from_name',''), //名称
                'MAIL_HOST' => input('host',''),//邮件发送SMTP服务器
                'MAIL_PORT' => input('port',465),
                'MAIL_SMTPAUTH' => TRUE,
                'MAIL_USERNAME' => input('username',''),//SMTP服务器登陆用户名
                'MAIL_PASSWORD' => input('password',''),//SMTP服务器登陆密码
                'MAIL_SECURE' => 'ssl',
                'MAIL_CHARSET' => 'UTF-8',
                'MAIL_ISHTML' => TRUE,
            );
            $saveData = array(
                'title' => $title,
                'flag' => $flag,
                'value' => serialize($value),
                'is_serialize' => 1,
            );
            //先删后增
            db('system_config')->where(array('flag' => $flag))->delete();
            db('system_config')->insert($saveData);

            //删除配置缓存
            $this->delSystemConfig();

            $this->success('操作成功');
        }else{
            $this->assign('title',$title);
            $this->assign('config_flag',$flag);
            //获取值
            $data = db('system_config')->where(array('flag' => $flag))->find();
            if($data && $data['is_serialize']){
                $data['value'] = unserialize($data['value']);
            }
            $this->assign('data',$data);
            return $this->fetch('common@system_config/mailer');
        }
    }

    /**
     * 阿里云短信服务参数
     */
    public function aliyunSms(){
        $title = '阿里云短信配置';
        $flag = GlobalConfig::ALIYUN_SMS;
        if(request()->isPost()){
            $value = array(
                'access_key_id' => input('access_key_id',''),
                'access_key_secret' => input('access_key_secret',''),
                'sign_name' => input('sign_name',''),
                'verify_template_code' => input('verify_template_code',''),
            );
            $saveData = array(
                'title' => $title,
                'flag' => $flag,
                'value' => serialize($value),
                'is_serialize' => 1,
            );
            //先删后增
            db('system_config')->where(array('flag' => $flag))->delete();
            db('system_config')->insert($saveData);

            //删除配置缓存
            $this->delSystemConfig();

            $this->success('操作成功');
        }else{
            $this->assign('title',$title);
            $this->assign('config_flag',$flag);
            //获取值
            $data = db('system_config')->where(array('flag' => $flag))->find();
            if($data && $data['is_serialize']){
                $data['value'] = unserialize($data['value']);
            }
            $this->assign('data',$data);
            return $this->fetch('common@system_config/aliyun_sms');
        }
    }

}